#include "gitservercore.h"
#include "gitservermain.h"
#include "gitdaemon.h"
#include "settings.h"
#include "settingsdialog.h"
#include <QtGui/QPainter>
#include <QtGui/QMenu>
#include <QtGui/QSystemTrayIcon>
#include <QtGui/QApplication>

INIT_SINGLETON(GitServerCore);

GitServerCore::GitServerCore()
{
    m_mainWindow = NULL;
    m_settingsDialog = NULL;
    m_gitDaemon = new GitDaemon(this);

    m_systemTrayIcon = new QSystemTrayIcon(this);
    m_systemTrayIcon->setIcon(QIcon(":/images/git.png"));
    qApp->setWindowIcon(QIcon(":/images/git.png"));

    m_setMenu();

    // Initialisation du gitcontroller
    m_initGitDaemon();
    m_gitDaemonIsRunning(m_gitDaemon->isRunning());

    connect( m_gitDaemon, SIGNAL(message(QString)), this, SLOT(showMessage(QString)));
    connect( m_gitDaemon, SIGNAL(error(QString)),   this, SLOT(showError(QString)));
    connect( m_gitDaemon, SIGNAL(started()),  this, SLOT(onDaemonStarted()));
    connect( m_gitDaemon, SIGNAL(stopped()),  this, SLOT(onDaemonStopped()));
}

GitServerCore::~GitServerCore()
{
    if (m_mainWindow)
        delete m_mainWindow;
}

//--------------------------------------------------------------------------------------------------

QSystemTrayIcon* GitServerCore::systemTrayIcon() const
{
    return m_systemTrayIcon;
}

//--------------------------------------------------------------------------------------------------

GitDaemon* GitServerCore::daemon() const
{
    return m_gitDaemon;
}

//--------------------------------------------------------------------------------------------------

SettingsDialog* GitServerCore::settingsDialog()
{
    if (!m_settingsDialog) {
        m_settingsDialog = new SettingsDialog(m_gitDaemon);
        connect(m_settingsDialog, SIGNAL(destroyed()), this, SLOT(onSettingsDialogDestroyed()));
    }

    return m_settingsDialog;
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::m_initGitDaemon()
{
    Settings settings;
    m_gitDaemon->setExecutable(settings.gitExecutable());
    m_gitDaemon->setBasePath(settings.gitBasePath());
    m_gitDaemon->setExportAll(settings.mustExportAll());
    m_gitDaemon->setUseDefaultPort(settings.useDefaultGitPort());
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::m_setMenu()
{
    QMenu *menu = new QMenu();
    m_startAction = menu->addAction(tr("Start"), m_gitDaemon, SLOT(start()));
    m_stopAction  = menu->addAction(tr("Stop"), m_gitDaemon, SLOT(stop()));
    menu->addSeparator();
    menu->addAction(tr("Show..."), this, SLOT(onShow()));
    menu->addAction(tr("Settings..."), this, SLOT(onShowSettings()));
    menu->addSeparator();
    menu->addAction(tr("Quit"), qApp, SLOT(quit()));
    m_systemTrayIcon->setContextMenu(menu);
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::onShowSettings()
{
    if (!settingsDialog()->isVisible())
        settingsDialog()->open();
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::onShow()
{
    if (m_mainWindow)
        return;

    m_mainWindow = new GitServerMain();
    m_mainWindow->setAttribute(Qt::WA_DeleteOnClose);
    m_mainWindow->show();
    connect(m_mainWindow, SIGNAL(destroyed()), this, SLOT(onMainWindowDestroyed()));
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::onMainWindowDestroyed()
{
    m_mainWindow = NULL;
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::onSettingsDialogDestroyed()
{
    m_mainWindow = NULL;
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::onDaemonStarted()
{
    m_gitDaemonIsRunning(true);
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::onDaemonStopped()
{
    m_gitDaemonIsRunning(false);
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::m_gitDaemonIsRunning(bool running)
{
    m_startAction->setEnabled(!running);
    m_stopAction->setEnabled(running);

    QPixmap icon = QPixmap(":/images/git.png");
    int marginX = icon.width() / 5;
    int marginY = icon.width() / 5;

    // Paint a red cross
    if (!running) {
        QPainter p(&icon);
        p.setPen(QPen(Qt::red, 25));
        p.drawLine(marginX, marginY, icon.width() - marginX,  icon.height() - marginY);
        p.drawLine(icon.width() - marginX, marginY, marginX, icon.height() - marginY);
    }

    m_systemTrayIcon->setIcon(icon);
    qApp->setWindowIcon(icon);
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::showMessage(const QString & msg)
{
    m_systemTrayIcon->showMessage(tr("Git Server"), msg, QSystemTrayIcon::NoIcon);
}

//--------------------------------------------------------------------------------------------------

void GitServerCore::showError(const QString & err)
{
    m_systemTrayIcon->showMessage(tr("Git Server"), err, QSystemTrayIcon::Critical);
}


